#include "commonpage.h"
#include "ui_commonpage.h"

#include <QDebug>
#include <QModelIndex>
#include <QListWidget>
#include "listitembox.h"

CommonPage::CommonPage(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::CommonPage)
{
    ui->setupUi(this);

    ui->pageMusicList->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    connect(ui->playButton, &QPushButton::clicked, this, [=](){
        emit playAll(pageType);
    });

    connect(ui->pageMusicList, &QListWidget::doubleClicked, this, [=](const QModelIndex& index){
        emit playMusicByIndex(this, index.row());
    });
}

CommonPage::~CommonPage()
{
    delete ui;
}

void CommonPage::setCommonPageUi(const QString &pageTitle, const QString &imagePath)
{
    ui->PageTitle->setText(pageTitle);

    ui->musicImageLabel->setPixmap(QPixmap(imagePath));
    ui->musicImageLabel->setScaledContents(true);
}

void CommonPage::setMusicListType(const PageType & type)
{
    pageType = type;
}

//将传进来的歌单添加到commonpage的ID队列中
void CommonPage::addMusicToCommonPage(MusicList& musiclist)
{
    musicOfPage.clear(); //这里清除我们musicOfPage中的内容和界面不显示无关
    ui->pageMusicList->clear();

    for(auto& music : musiclist) //需要提供begin 和 end 方法
    {
        switch(pageType)
        {
        case LIKE_PAGE:
            if(music.getIsLike())
                musicOfPage.push_back(music.getMusicId());
            break;
        case LOCAL_PAGE:
                musicOfPage.push_back(music.getMusicId());
            break;
        case RECENT_PAGE:
            if(music.getIsHistory())
                musicOfPage.push_back(music.getMusicId());
            break;
        case SEARCH_PAGE:
            if(music.getIsSearched())
                musicOfPage.push_back(music.getMusicId());
            break;
        default:
            qDebug() << "暂未支持";
            break;
        }
    }
}

void CommonPage::refresh(MusicList & musiclist)
{
    addMusicToCommonPage(musiclist);

    for(auto musicId : musicOfPage)
    {
        auto it = musiclist.findMusicByMusicId(musicId);

        ListItemBox* listItemBox = new ListItemBox(ui->pageMusicList);
        QListWidgetItem* item = new QListWidgetItem(ui->pageMusicList);
        items.insert(item);

        item->setSizeHint(QSize(listItemBox->width(), listItemBox->height()));
        listItemBox->setMusicNameLabel(it->getMusicName());
        listItemBox->setMusicSingerLabel(it->getMusicSinger());
        listItemBox->setMusicAlbumLabel(it->getMusicAlbum());
        listItemBox->setLikeMusic(it->getIsLike());
        listItemBox->setUrl(musicId);

        ui->pageMusicList->setItemWidget(item, listItemBox);
        //将repaint时间放在Qt的时间循环队列中，然后返回，没有立马处理
        //update();
        //立刻响应paintEvent事件

        connect(listItemBox, &ListItemBox::setIsLike, this, [=](bool isLike){
            emit updateLikeMusic(isLike, it->getMusicId());
        });

        repaint();

        //listItemBox->deleteLater();
    }
}

//更新pageMusicList中的组件


void CommonPage::addMusicToPlaylist(MusicList &musicList, QMediaPlaylist *playlist)
{
    for(auto& music : musicList)
    {
        switch(pageType)
        {
        case LIKE_PAGE:
            if(music.getIsLike())
            {
                playlist->addMedia(music.getMusicUrl());
            }
            break;
        case LOCAL_PAGE:
            playlist->addMedia(music.getMusicUrl());
            break;
        case RECENT_PAGE:
            if(music.getIsHistory())
            {
                playlist->addMedia(music.getMusicUrl());
            }
            break;
        case SEARCH_PAGE:
            if(music.getIsSearched())
            {
                playlist->addMedia(music.getMusicUrl());
            }
            break;
        default:
            qDebug() << "尚未支持";
        }
    }
}

const QString &CommonPage::getMusicByIndex(int index)
{
    if(index > musicOfPage.size())
    {
        return "";
    }
    return musicOfPage[index];
}

void CommonPage::updateMusicCover(QVariant imageVariant)
{

    if(imageVariant.isValid())
    {
        QImage image = imageVariant.value<QImage>();
        ui->musicImageLabel->setPixmap(QPixmap::fromImage(image));
    }
    else
    {
        QString path(":/images/non-music.png");
        ui->musicImageLabel->setPixmap(path);
    }
    ui->musicImageLabel->setScaledContents(true);
}

void CommonPage::initSearchPage()
{
    if(pageType == SEARCH_PAGE)
    {
        ui->PageTitle->setText("搜索结果");
        ui->musicImageLabel->setPixmap(QPixmap(":/images/sousuo.png"));
        ui->musicImageLabel->setScaledContents(true);
        //ui->musicImageLabel->setStyleSheet("background-position: center, center");
    }
}
